package com.bt.framework.utils

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.telephony.TelephonyManager

/**
 *@author:hxm
 *@date: 2025/6/24 23:16
 *@version:
 *@description:网络工具类
 */
object NetWorkUtil {

    /**
     * 判断网络是否连接
     */
    @SuppressLint("WrongConstant", "ObsoleteSdkInt")
    fun isConnected(context: Context): Boolean {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        return (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = cm.getNetworkCapabilities(cm.activeNetwork)
            networkCapabilities?.hasTransport(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true
        } else {
            //只判断是否有网络连接
            cm.activeNetworkInfo?.isConnected == true
        })
    }

    /**
     * 获取当前网络类型
     */
    @SuppressLint("MissingPermission", "ObsoleteSdkInt")
    fun getNetworkType(context: Context): NetworkType {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val isWiFi = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val networkCapabilities = cm.getNetworkCapabilities(cm.activeNetwork)
            networkCapabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true
        } else {
            cm.activeNetworkInfo?.type == ConnectivityManager.TYPE_WIFI
        }
        if (isWiFi) {
            return NetworkType.NETWORK_WIFI
        }

        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return when (tm.networkType) {
            TelephonyManager.NETWORK_TYPE_GPRS,
            TelephonyManager.NETWORK_TYPE_GSM,
            TelephonyManager.NETWORK_TYPE_EDGE,
            TelephonyManager.NETWORK_TYPE_CDMA,
            TelephonyManager.NETWORK_TYPE_1xRTT,
            TelephonyManager.NETWORK_TYPE_IDEN,
                -> NetworkType.NETWORK_2G

            TelephonyManager.NETWORK_TYPE_UMTS,
            TelephonyManager.NETWORK_TYPE_EVDO_0,
            TelephonyManager.NETWORK_TYPE_EVDO_A,
            TelephonyManager.NETWORK_TYPE_HSDPA,
            TelephonyManager.NETWORK_TYPE_HSUPA,
            TelephonyManager.NETWORK_TYPE_HSPA,
            TelephonyManager.NETWORK_TYPE_EVDO_B,
            TelephonyManager.NETWORK_TYPE_EHRPD,
            TelephonyManager.NETWORK_TYPE_HSPAP,
            TelephonyManager.NETWORK_TYPE_TD_SCDMA
                -> NetworkType.NETWORK_3G

            TelephonyManager.NETWORK_TYPE_LTE,
                -> NetworkType.NETWORK_4G

            TelephonyManager.NETWORK_TYPE_NR
                -> NetworkType.NETWORK_5G

            TelephonyManager.NETWORK_TYPE_IWLAN
                -> NetworkType.NETWORK_5G

            else ->
                NetworkType.NETWORK_UNKNOWN
        }
    }

    /**
     * 获取运营商名称
     */
    fun getOperatorName(context: Context): String {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return tm.simOperatorName
    }

    /**
     * 是否弱网 < 4G
     */
    fun isWeakNetwork(context: Context): Boolean {
        if (!isWeakNetwork(context)) {
            return true
        }
        val type = getNetworkType(context)
        return type != NetworkType.NETWORK_WIFI &&
                type != NetworkType.NETWORK_5G &&
                type != NetworkType.NETWORK_4G
    }

}


/**
 * 网络类型
 */
enum class NetworkType {
    NETWORK_UNKNOWN,
    NETWORK_WIFI,
    NETWORK_2G,
    NETWORK_3G,
    NETWORK_4G,
    NETWORK_5G
}